<%--
  Created by IntelliJ IDEA.
  User: piress
  Date: 2017/5/23
  Time: 16:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>注册</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- CSS -->
    <link rel="stylesheet" href="/css/reset.css">
    <link rel="stylesheet" href="/css/supersized.css">
    <link rel="stylesheet" href="/css/style.css">

    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

</head>
<body>

<div class="page-container">
    <h1>注册</h1>
    <div class="forms">
        <div id="error" style="display:none; color:#F00" ></div>
        <input type="text" id="username" class="username" placeholder="电子邮箱">
        <input type="password" id="password" class="password" placeholder="密码">
        <input type="password" id="passwords" class="password" placeholder="确认密码">
        <input type="text" id="nickname" class="username" placeholder="昵称">
        <button id="submit">提交</button>
    </div>
    <%--<div class="connect">--%>
        <%--<p>Or connect with:</p>--%>
        <%--<p>--%>
            <%--<a class="facebook" href=""></a>--%>
            <%--<a class="twitter" href=""></a>--%>
        <%--</p>--%>
    <%--</div>--%>
</div>

<!-- Javascript -->
<script src="/js/jquery-1.8.2.min.js"></script>
<script src="/js/supersized.3.2.7.min.js"></script>
<script src="/js/supersized-init.js"></script>
<script src="/js/scripts.js"></script>
<script>
    $(function(){

    });

    $("input").focus(function(){
        $("#error").text("");
        $("#error").hide();
    });


    function testEmail(email){
        var pattern= /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        var strEmail=pattern.test(email);
        return strEmail;
    }

    function testPhone(phone){
        var pattern=/^((13[0-9])|(15[1-3,5-9])|(17[7])|(18[0-9]))\d{8}$/;
        var strPhone=pattern.test(phone);
        return strPhone;
    }

    $("#username").blur(function(){
        var username = $("#username").val();
        if(!testEmail(username)){
            $("#error").text("请输入正确的邮箱地址");
            $("#error").show();
            return;
        }
        $.ajax({
            type : 'GET',
            url : '/checkUser',
            data : {
                "username" : username
            },
            dataType : 'json',
            success : function(data) {
                if(data.code==200){

                }else{
                    $("#error").text(data.msg);
                    $("#error").show();
                }
            }
        });
    });

    $("#passwords").blur(function(){
        var password = $("#password").val();
        var passwords = $("#passwords").val();

        if(passwords != password){
            $("#error").text("两次密码不一致");
            $("#error").show();
        }

    });

    $("#submit").click(function(){

        var password = $("#password").val();
        var username = $("#username").val();
        var nickname = $("#nickname").val();
        var passwords = $("#passwords").val();

        if(!testEmail(username)){
            $("#error").text("请输入正确的邮箱地址");
            $("#error").show();
            return;
        }

        if(passwords != password){
            $("#error").text("两次密码不一致");
            $("#error").show();
            return;
        }

        $.ajax({
            type : 'GET',
            url : '/toregister',
            data : {
                "username" : username,
                "nickname" : nickname,
                "password" : password
            },
            dataType : 'json',
            success : function(data) {
                if(data.code==200){
                    window.location.href='/';
                }else{
                    console.log(data.msg);
                    $("#error").text(data.msg);
                    $("#error").show();
                }
            }
        });
    })
</script>
</body>
</html>
